//
//  timeTableView.m
//  ElektroM
//
//  Created by Michael on 12.10.09.
//  Copyright 2009 Artgruppe inc. All rights reserved.
//

#import "detailedTimeTable.h"
#import "railwaysController.h"
#import "dataBase.h"
#import "AtomicElementViewController.h"
#import "Train.h"
#import "Constants.h"
#import "timeTableAnother.h"


@implementation detailedTimeTable
@synthesize _sDataBaseName;
@synthesize tableView;
@synthesize strTitle;
@synthesize tabBarController;

@synthesize TrainId;

-(void)updateData
{
	// here we would use database for showing up a time table:
	if(_db==nil){
		_db=[dataBase alloc];		
		_db.namePrefix=_sDataBaseName;	
		_db.databaseName=[_sDataBaseName stringByAppendingString:@"Railways.sqlite"];	
	}
		
	// read out data from the database:
	[_db initialization];
	_aiTrainTimes = [_db readTrainInfoAsNSArray: TrainId];
	_aiTrainStations=[_db getTrainStations];
	_aiStationsNames=[_db getTrainStationsNames];	
	
	[tableView reloadData];	
}

 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
		
    }	
    return self;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
 [self setTitle: NSLocalizedString(@"Route", @"")];	
    [super viewDidLoad];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

#pragma mark -
#pragma mark UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	//printf("in number of rows: nNeededForMeCount %d\n", nNeededForMeCount);
	return [_aiTrainTimes count];
}

- (UITableViewCell *)tableView:(UITableView *)tableViewMy cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *kCellIdentifier = @"cellID";
	
	UITableViewCell *cell = [tableViewMy dequeueReusableCellWithIdentifier:kCellIdentifier];
	if (!cell)
	{
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kCellIdentifier] autorelease];
        
		cell.accessoryType = UITableViewCellAccessoryNone;
		//UITableViewCellAccessoryDisclosureIndicator;
        
        cell.textLabel.backgroundColor = [UIColor clearColor];
		cell.textLabel.opaque = NO;
		cell.textLabel.textColor = [UIColor blackColor];
		cell.textLabel.highlightedTextColor = [UIColor whiteColor];
		cell.textLabel.font = [UIFont boldSystemFontOfSize:18];
		
		cell.detailTextLabel.backgroundColor = [UIColor clearColor];
		cell.detailTextLabel.opaque = NO;
		cell.detailTextLabel.textColor = [UIColor grayColor];
		cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
		cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
    }
    if(!_bReleaseMySelf){
	// get info about time table here...   
	cell.textLabel.text = [_aiTrainTimes objectAtIndex: indexPath.row];
    
	int indexSt=[[_aiTrainStations objectAtIndex: indexPath.row] intValue]; 
	if(indexSt!=0)
	    cell.detailTextLabel.text=[_aiStationsNames objectAtIndex: indexSt-1];
	else
	    cell.detailTextLabel.text=NSLocalizedString(@"NameOutOfDirection", @"");	
	}
	else
	{
		cell.textLabel.text=@"None";
		cell.detailTextLabel.text=@"??.??";
		
	}

	return cell;
}

#pragma mark -
#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}

#pragma mark -
#pragma mark BarReleaseStuff
- (void)viewWillAppear:(BOOL)animated{
	_bReleaseMySelf=false;
	//printf("View appeared!\n");
	//[tableView removeFromSuperview];
	// kick him off, that bustard!
	if(!tabBarController.tabBar.hidden)
	{
		tabBarController.tabBar.hidden=YES;		
	}	
}

- (void)viewWillDisappear:(BOOL)animated{
	_bReleaseMySelf=true;
	//printf("View disappeared!\n");	
    if(_db !=nil){
		_db.namePrefix=nil;
		_db.databaseName=nil;
		[_db cleanTrainInfoAsNSArray];
		[_db release];
		_db=nil;
    }    
}

#pragma mark -
#pragma mark Memory Releases
- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
		
    if(_db !=nil)
    {
		_db.namePrefix=nil;
		_db.databaseName=nil;	
		[_db cleanTrainInfoAsNSArray];
		[_db release];
		_db=nil;
    }
    if(tableView!=nil)
	[tableView release];
    tableView = nil;	
    if(_sDataBaseName!=nil)
	[_sDataBaseName release];
    _sDataBaseName = nil;
    if(strTitle!=nil)
	[strTitle release];
    strTitle = nil;		
}

- (void)dealloc {	
    [super dealloc];
}

@end
